package com.hr.system.service.impl;

import java.util.Date;
import java.util.List;

import com.hr.common.core.domain.Result;
import com.hr.common.utils.DateUtils;
import com.hr.common.utils.ResultUtil;
import com.hr.common.utils.ShiroUtils;
import com.hr.system.domain.ClockRule;
import com.hr.system.service.IClockRuleService;
import com.hr.system.utils.ClockUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hr.system.mapper.SysPersonnelClockMapper;
import com.hr.system.domain.SysPersonnelClock;
import com.hr.system.service.ISysPersonnelClockService;
import com.hr.common.core.text.Convert;

/**
 * 打卡Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-01-18
 */
@Service
public class SysPersonnelClockServiceImpl implements ISysPersonnelClockService 
{
    @Autowired
    private SysPersonnelClockMapper sysPersonnelClockMapper;

    @Autowired
    private IClockRuleService clockRuleService;

    @Override
    public SysPersonnelClock selectSysPersonnelClockById(Integer id)
    {
        return sysPersonnelClockMapper.selectSysPersonnelClockById(id);
    }


    @Override
    public List<SysPersonnelClock> selectSysPersonnelClockList(SysPersonnelClock personnelClock)
    {
        return sysPersonnelClockMapper.selectSysPersonnelClockList(personnelClock);
    }


    @Override
    public Result<SysPersonnelClock> insertSysPersonnelClock(SysPersonnelClock sysPersonnelClock)
    {
        //当前用户id
        Long personnelId = sysPersonnelClock.getPersonnelId().longValue();
        //当前日期
        String currentDateStr = DateUtils.getDate();
        //先查询当前日期自己是否已有打卡
        SysPersonnelClock currentDateClock = sysPersonnelClockMapper.selectSysPersonnelClockByClockDate(personnelId, currentDateStr);
        //若当日没有打卡
        if(currentDateClock == null){
            sysPersonnelClock.setClockDate(DateUtils.getNowDate());
            sysPersonnelClock.setClockWeek(DateUtils.getWeek(new Date()));
            sysPersonnelClock.setNumberOdClocks(1);
            sysPersonnelClock.setClockTime(DateUtils.getCurrentTime());
            int updateNum = sysPersonnelClockMapper.insertSysPersonnelClock(sysPersonnelClock);
            if (updateNum > 0){
                return ResultUtil.success(null);
            }else{
                return ResultUtil.error(500,"打卡失败");
            }
        }else {
            //打卡次数加一
            Integer numberClock = currentDateClock.getNumberOdClocks();
            if (numberClock != null){
                currentDateClock.setNumberOdClocks(currentDateClock.getNumberOdClocks()+1);
            }else {
                currentDateClock.setNumberOdClocks(1);
            }
            //打卡时间更新
            if ("".equals(currentDateClock.getClockTime()) || currentDateClock.getClockTime() == null){
                currentDateClock.setClockTime(DateUtils.getCurrentTime());
            }else {
                currentDateClock.setClockTime(currentDateClock.getClockTime()+", "+DateUtils.getCurrentTime());
            }
            //更新到数据库
            int updateNum = sysPersonnelClockMapper.updateSysPersonnelClock(currentDateClock);
            //返回前端
            if (updateNum > 0){
                return ResultUtil.success(null);
            }else{
                return ResultUtil.error(500,"打卡失败");
            }
        }
    }


    @Override
    public int updateSysPersonnelClock(SysPersonnelClock sysPersonnelClock)
    {
        return sysPersonnelClockMapper.updateSysPersonnelClock(sysPersonnelClock);
    }


    @Override
    public int deleteSysPersonnelClockByIds(String ids)
    {
        return sysPersonnelClockMapper.deleteSysPersonnelClockByIds(Convert.toStrArray(ids));
    }


    @Override
    public int deleteSysPersonnelClockById(Integer id)
    {
        return sysPersonnelClockMapper.deleteSysPersonnelClockById(id);
    }

    @Override
    public SysPersonnelClock selectSysPersonnelClockByClockDate(Long userId, String currentDate) {
        return sysPersonnelClockMapper.selectSysPersonnelClockByClockDate(userId, currentDate);
    }


    @Override
    public List<SysPersonnelClock> selectSysPersonnelAbnormalClocks(SysPersonnelClock abnormalPersonnelClock) {
        List<SysPersonnelClock> abnormalClocks = sysPersonnelClockMapper.selectSysPersonnelAbnormalClocks(abnormalPersonnelClock);
        return abnormalClocks;
    }

    @Override
    public List<SysPersonnelClock> selectClockCurrentDate() {
        return sysPersonnelClockMapper.selectClockCurrentDate();
    }

    @Override
    public int updateCurrentDateClocks() {
        int updateColNum = 0;
        //查出当前日期的打卡数据
        List<SysPersonnelClock> clocksCurrentDate = sysPersonnelClockMapper.selectClockCurrentDate();
        if (clocksCurrentDate != null && clocksCurrentDate.size() > 0) {
            //遍历
            for (SysPersonnelClock currentDatePersonnelClock : clocksCurrentDate) {
                //当前日期
                String currentDate = DateUtils.getCurrentTime();
                //当前年份
                String currentYear = DateUtils.getCurentYear();
                //当前星期
                String nowWeek = DateUtils.getNowWeek();
                //当前打卡租户id
                Integer tenantId = currentDatePersonnelClock.getTenantId();
                //打卡规则
                ClockRule queryRule = new ClockRule();
                queryRule.setTenantId(tenantId.longValue());
                queryRule.setSuitYear(currentYear);
                //对应租户的打卡规则
                ClockRule clockRule = clockRuleService.selectClockRuleByYearAndTenantId(queryRule);
                List<String> allFreeDays = ClockUtils.getAllFreeDays(clockRule);
                //如果是非工作日或者假期，就不需要执行更新
                if (allFreeDays.contains(currentDate)) {
                    return 0;
                }
                List<String> notWorkWeeks = ClockUtils.getNotWorkWeeks(clockRule);
                if (notWorkWeeks.contains(nowWeek)) {
                    return 0;
                }
                //根据打卡规则更新状态
                int clockCode = ClockUtils.setClockState(currentDatePersonnelClock, clockRule);
                currentDatePersonnelClock.setClockState(clockCode);
                //执行更新
                int i = sysPersonnelClockMapper.updateSysPersonnelClock(currentDatePersonnelClock);
                //影响行数累加
                updateColNum += i;
            }
        }
        return updateColNum;
    }

    @Override
    public int batchInsertClock(List<SysPersonnelClock> SysPersonnelClockList) {
        return sysPersonnelClockMapper.batchInsertClock(SysPersonnelClockList);
    }

    @Override
    public int getErrClockTimes(SysPersonnelClock clock) {
        return sysPersonnelClockMapper.getErrClockTimes(clock);
    }

    @Override
    public SysPersonnelClock getYesterdayPersonnelClock(SysPersonnelClock clock) {
        SysPersonnelClock yesterdayPersonnelClock = sysPersonnelClockMapper.getYesterdayPersonnelClock(clock);
        return yesterdayPersonnelClock;
    }

}
